package com.example.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.entity.SysMenu;
import com.example.auth.entity.dto.SysMenuCreateDTO;
import com.example.auth.entity.dto.SysMenuEditDTO;
import com.example.auth.entity.dto.SysRoleQueryDTO;
import com.example.auth.entity.vo.SysMenuQueryVO;
import com.example.auth.entity.vo.SysRoleQueryVO;
import com.example.auth.service.SysMenuService;
import com.example.auth.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: LiYang
 * @create: 2023-07-17 16:33
 * @Description:
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("system/menu")
@Validated
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @GetMapping("findNodes")
    @ApiOperation("查询菜单")
    public Result<List<SysMenu>> findNodes(){
        List<SysMenu> sysMenuList = sysMenuService.findNodes();
        return Result.success(sysMenuList);
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @PostMapping("insertMenu")
    @ApiOperation("添加菜单")
    public Result<Void> insertMenu(@RequestBody @Valid SysMenuCreateDTO sysMenuCreateDTO){
        sysMenuService.insertMenu(sysMenuCreateDTO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @GetMapping("getMenuById/{id}")
    @ApiOperation("通过id查询菜单")
    public Result<SysMenuQueryVO> getMenuById(@PathVariable Long id){
        SysMenuQueryVO sysMenuQueryVO = sysMenuService.getMenuById(id);
        return Result.success(sysMenuQueryVO);
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @PostMapping("updateMenu")
    @ApiOperation("修改菜单")
    public Result<Void>  updateMenu(@RequestBody @Valid SysMenuEditDTO sysMenuEditDTO){
        sysMenuService.updateMenu(sysMenuEditDTO);
        return Result.success();
    }


    @GetMapping("getParentMenuByParentId/{parentId}")
    @ApiOperation("通过parentId查询父菜单")
    public Result<SysMenuQueryVO> getParentMenuByParentId(@PathVariable Long parentId){
        SysMenuQueryVO sysMenuQueryVO = sysMenuService.getParentMenuByParentId(parentId);
        return Result.success(sysMenuQueryVO);
    }

    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @DeleteMapping("removeMenuById/{id}")
    @ApiOperation("通过id删除菜单")
    public Result<Void>  removeMenuById(@PathVariable Long id){
        sysMenuService.removeMenuById(id);
        return Result.success();
    }
}
